<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@taglib prefix="s" uri="/struts-tags"%>
<%@ page
	import="com.onlineMIS.common.Common_util,java.util.Date,java.text.SimpleDateFormat"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>朴与素连锁店管理信息系统</title>
<%@ include file="../../common/Style.jsp"%>

<script>


var baseurl = "<%=request.getContextPath()%>";
$(document).ready(function(){
	parent.$.messager.progress('close'); 
	$.messager.progress({
		title : '提示',
		text : '数据处理中，请稍后....'
	});
	
	var params= $.serializeObject($('#preGenReportForm'));
	$('#dataGrid').treegrid({
		url : 'headQReportJSON!getSalesStatisticReptEles',
		idField: 'id',
		queryParams: params,
		treeField : 'name',
		rownumbers: true,
		lines : true,

		onLoadSuccess : function(row, param){
			$.messager.progress('close'); 
		},
		onBeforeExpand : function(node) {
			$("#parentId").val( node.parentId);
		    $("#yearId").val( node.yearId);
			$("#quarterId").val( node.quarterId);
			$("#brandId").val( node.brandId);
			$("#categoryLevelId").val(node.categoryLevelId);
			$("#categoryId").val(node.categoryId);
			$("#pbId").val(node.pbId);
			var params = $('#preGenReportForm').serialize();
			$('#dataGrid').treegrid('options').url = 'headQReportJSON!getSalesStatisticReptEles?' + params;
		},	
		rowStyler: function(row){
            var style = "";
            if (row.isChain == true)
            	style = "background-color:#EBEDEF;color:blue;";
			return style;
		},
		frozenColumns :[[					
						{field:'name', width:240,title:' <s:property value="formBean.startDate"/>到 <s:property value="formBean.endDate"/>',
							formatter: function (value, row, index){
								if (row.state == 'open' && row.chainId != -1) {
									var str = '';
								    str += $.formatString('<a href="#" onclick="traceInventory(\'{0}\');">{1}</a>', row.barcode, row.name);
								    return str;
								} else 
									return row.name;
							}}
							
							]],
		columns : [ [
				{field:'img',width:60,title:'图片',
					formatter : function(value, row, index) {
						if(row.img != ""){
							var str = '';
							  str += $.formatString('<a href="#" onclick="viewImg({0});">{1}</a>', row.barcode,row.img);
							return str;
						}else {
							return row.img;
						}
						
					}},
					{field:'salesQ', width:55,title:'销售量A'},
					{field:'returnQ', width:55,title:'退货量B'},
					{field:'netQ', width:70,title:'净售量A-B'},
					{field:'salesPrice', width:70,title:'销售额C',
						formatter: function (value, row, index){
							return (row.salesPrice).toFixed(2);
						}
					},
					{field:'returnPrice', width:70,title:'退货额D',
						formatter: function (value, row, index){
							return (row.returnPrice).toFixed(2);
						}
					},
					{field:'netPrice', width:75,title:'净售额C-D',
						formatter: function (value, row, index){
							return (row.netPrice).toFixed(2);
						}
					},{field:'salesCost', width:70,title:'销售成本E',
						formatter: function (value, row, index){							
							return (row.salesCost).toFixed(2);

					}},{field:'returnCost', width:70,title:'退货成本F',
						formatter: function (value, row, index){							
							return (row.returnCost).toFixed(2);

					}},
					{field:'netCost', width:80,title:'净售成本E-F',
						formatter: function (value, row, index){							
								return (row.netCost).toFixed(2);

						}},
					{field:'netProfit', width:110,title:'商品利润C-D-E-F',
						formatter: function (value, row, index){
								return (row.netProfit).toFixed(2);
						}
					}
			     ]],
		toolbar : '#toolbar',
	});
});

function refresh(){
	$("#parentId").val( 0);
    $("#yearId").val( 0);
	$("#quarterId").val( 0);
	$("#brandId").val( 0);
	$("#categoryLevelId").val(0);
	$("#categoryId").val(0);
    document.preGenReportForm.action="headqReportJSP!preGenerateHqSalesReportDetails";
    document.preGenReportForm.submit();
}
function back(){
    document.preGenReportForm.action="headqReportJSP!preGenerateHqSalesReport";
    document.preGenReportForm.submit();
}
function exportFile(){
	
	var node = $('#dataGrid').treegrid('getSelected');

	if (node == null){
		$.messager.alert('错误', '请先选中一行再继续操作', 'error');
	} else {

		$("#chainId").val( node.chainId);
	    $("#yearId").val( node.yearId);
		$("#quarterId").val( node.quarterId);
		$("#brandId").val( node.brandId);
		$("#categoryLevelId").val(node.categoryLevelId);
		$("#categoryId").val(node.categoryId);
        document.preGenReportForm.action="headqReportJSP!generateHqSalesStatisticExcelReport";
        document.preGenReportForm.submit();
	}
}

function exportDetailFile(){
	var node = $('#dataGrid').treegrid('getSelected');

	if (node == null){
		$.messager.alert('错误', '请先选中一行再继续操作', 'error');
	} else {
		$.messager.confirm('功能确认', '如果日期选择过长,此功能会下载大量数据，如果在繁忙时间可能会拖垮服务器.你确定继续?', function(r){
			if (r){
				$("#chainId").val( node.chainId);
			    $("#yearId").val( node.yearId);
				$("#quarterId").val( node.quarterId);
				$("#brandId").val( node.brandId);
				$("#categoryLevelId").val(node.categoryLevelId);
				$("#categoryId").val(node.categoryId);
		        document.preGenReportForm.action="headqReportJSP!generateHqSalesStatisticExcelDetailReport";
		        document.preGenReportForm.submit();
			}
		});

	}
}

//下载层级报表
function exportLevelFile(){
	var node = $('#dataGrid').treegrid('getSelected');

	if (node == null){
		$.messager.alert('错误', '请先选中一行再继续操作', 'error');
	} else {
		$("#chainId").val( node.chainId);
	    $("#yearId").val( node.yearId);
		$("#quarterId").val( node.quarterId);
		$("#brandId").val( node.brandId);
		$("#categoryLevelId").val(node.categoryLevelId);
		$("#categoryId").val(node.categoryId);
		$("#parentId").val(1);
        document.preGenReportForm.action="headqReportJSP!generateHqSalesStatisticLevelReport";
        document.preGenReportForm.submit();				
	}
}

/**
 * 货品销售记录
 */
function traceInventory(barcode){
	var custId = $("#custId").val();
	var startDate = $("#startDate").val();
	var endDate = $("#endDate").val();
	var params = "formBean.barcode="+ barcode +"&formBean.cust.id="+custId +"&formBean.startDate="+startDate +"&formBean.endDate="+endDate;
			$.modalDialog({
				title : barcode + " 货品信息跟踪",
				width : 540,
				height : 220,
				modal : false,
				draggable:true,
				href : 'headqReportJSP!selectSalesRecords?' + params,
		});		
	
}

</script>
</head>
<body>
	<div class="easyui-layout" data-options="fit : true,border : false">
		<div data-options="region:'north',border:false" style="height: fit">
			<s:form id="preGenReportForm" name="preGenReportForm" action=""
				theme="simple" method="POST">
				<s:hidden name="formBean.parentId" id="parentId" />
				<s:hidden name="formBean.startDate" id="startDate" />
				<s:hidden name="formBean.endDate" id="endDate" />
				<s:hidden name="formBean.cust.id" id="custId" />
				<s:hidden name="formBean.barcode" id="barcode"/>
				<s:hidden name="formBean.reportType" id="reportType"/>
				<s:hidden name="formBean.belongTo" id="belongTo"/>
				<input type="hidden" name="formBean.year.year_ID" id="yearId"
					value="0" />
				<input type="hidden" name="formBean.quarter.quarter_ID"
					id="quarterId" value="0" />
				<input type="hidden" name="formBean.brand.brand_ID" id="brandId"
					value="0" />
				<input type="hidden" name="formBean.categoryLevel.category_ID" id="categoryLevelId" value="0"/>
            	<input type="hidden"  name="formBean.category.category_ID" id="categoryId" value="0"/>
            	<s:hidden name="formBean.rptTypeId" id="rptTypeId"/>
			</s:form>
		</div>
		<div data-options="region:'center',border:false">
			<table id="dataGrid">
			</table>
			<div id="toolbar" style="display: none;">
				<a onclick="back();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-back'">退回上页</a> <a
					onclick="refresh();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-reload'">刷新</a> <a
					onclick="exportFile();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-print'">导出报表</a> <a
					onclick="exportDetailFile();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-print'">导出明晰报表</a>
					<a onclick="exportLevelFile();" href="javascript:void(0);"
					class="easyui-linkbutton"
					data-options="plain:true,iconCls:'icon-print'">导出层级报表</a>
					
					
					
			</div>
		</div>
	</div>
</body>
</html>